/**
 * @author David Young
 */
package csab;

import java.io.IOException;

import javax.swing.JOptionPane;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import csab.core.PlayerStats;
import csab.core.utility.NameGenerator;

/**
 * The entry point for this application.
 * @author David Young
 */
public class EntryPoint extends StateBasedGame {
	public static final int MENUSTATE_ID = 0;
	public static final int PLAYSTATE_ID = 1;
	public static final int OPTIONSSTATE_ID = 2;
	/**
	 * @param name
	 */
	public EntryPoint() {
		super("Entry Point");
	}

	/* (non-Javadoc)
	 * @see org.newdawn.slick.state.StateBasedGame#initStatesList(org.newdawn.slick.GameContainer)
	 */
	@Override
	public void initStatesList(GameContainer container) throws SlickException {
		this.addState(new MenuState(MENUSTATE_ID));
		this.addState(new PlayState(PLAYSTATE_ID));
		this.addState(new OptionMenuState(OPTIONSSTATE_ID));
		this.enterState(MENUSTATE_ID);
	}
	
	public static void main(String [] args) throws SlickException, IOException
	{
		if(PlayerStats.getPlayerName().compareToIgnoreCase("") == 0)
		{
			String input = JOptionPane.showInputDialog(null, 
					"Please choose a name for your player.", 
					"Choose a name", JOptionPane.QUESTION_MESSAGE);
			PlayerStats.setPlayerName(input);
			NameGenerator generator = new NameGenerator(input);
			PlayerStats.setGameName(generator.generateName());
			generator.release();
			PlayerStats.save();
		}
		
		boolean dofscreen = JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, 
				"Would you like to play the game in fullscreen mode?", 
				"Fullscreen", JOptionPane.YES_NO_OPTION);
		
		AppGameContainer gc = new AppGameContainer(new EntryPoint());
		float aspect = (float)gc.getScreenWidth() / gc.getScreenHeight();
		//Calculate for aspect ratios
		float epsilon = 0.01f;
		if(Math.abs(aspect - (float)16/9) < epsilon)
			gc.setDisplayMode(1280, 720, false);
		else if(Math.abs(aspect - (float)4/3) < epsilon)
			gc.setDisplayMode(800, 600, false);
		else if(Math.abs(aspect - (float)16/10) < epsilon)
			gc.setDisplayMode(1280, 800, false);
		else {
			JOptionPane.showMessageDialog(null, 
					"Your computer is not of an aspect ratio "+
					"compatible with this game. Defaulting to 4:3.",
					"Unsupported Aspect Ratio", 
					JOptionPane.ERROR_MESSAGE);
			gc.setDisplayMode(800, 600, false);
		}
		gc.setTitle(PlayerStats.getGameName());
		gc.setShowFPS(false);
		gc.setClearEachFrame(false);
		gc.setUpdateOnlyWhenVisible(true);
		gc.setFullscreen(dofscreen);
		gc.start();
	}
}